<?php ob_start();?>
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/checkUser.php');
function goToProduct($success){
	echo '<script LANGUAGE="JavaScript">';
	echo 'window.location="/product?success='.$success.'";';
	echo '</script>';
}
$db = connect();

$productName = $_POST["product_name"];
$unit = $_POST["selectBox"];
$id = -1;
if(isset($_POST["product_id"])){
	$id = $_POST["product_id"];
}else{
	$id = $_GET["product_id"];
}
$note = $_POST["product_note"];
$disable = (isset($_POST["product_disable"]) ? 'E' : 'H');
$sameDay = (isset($_POST["product_sameday"]) ? 'E' : 'H');
$product_interval = $_POST["product_interval"];
$price = $_POST["product_price"];
$category = $_POST["product_category"];

$successFlag = 'E';
if($id <> null || $id <> ''){
	if(isset($_GET["DELETE"])){
		$stmt ="DELETE FROM MENU
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('i', $id);
		if($statement->execute ()) {
			echo '<span style="color:green; font-weight:bolder;">Kayıt başarıyla silindi</span>';
		} else {
			echo '<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>';
		}
		$statement->close();
	}else{
		$stmt ="UPDATE MENU 
					SET PRODUCT_NAME = ?, 
						UNIT = ?,
						ISDISABLED = ?,
						START_TIME = ?,
						END_TIME = ?,
						SAMEDAY = ?,
						NOTES = ?
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('sssssssi', $productName, $unit, $disable, split("/", $product_interval)[0], split("/", $product_interval)[1], $sameDay, $note, $id);
		if($statement->execute ()) {
			$successFlag = 'E';
		}else {
			$successFlag = 'H';
		}
		$statement->close();
	}
}else{
	if(trim($productName,' ') <> '' || trim($product_interval,' ') <> '' ){
		$stmt ="INSERT INTO MENU (PRODUCT_NAME, UNIT, ISDISABLED, START_TIME, END_TIME, SAMEDAY, NOTES) 
					VALUES(?,?,?,?,?,?,?)";
		$statement = $db->prepare($stmt);
		$statement->bind_param('sssssss', $productName, $unit, $disable, split("/", $product_interval)[0], split("/", $product_interval)[1], $sameDay, $note);
		if($statement->execute ()) {
			$successFlag = 'E';
		}else {
			$successFlag = 'H';
		}
		echo '<span id="insert_id" style="display:none;">'.$db->insert_id.'</span>';
		$id = $db->insert_id;
		$statement->close();
		
		$stmt ="INSERT INTO PRICE_LIST (MENU_ID, PRICE, VERSION) VALUES ($id, $price, 0)";
		$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
	}else{
		$successFlag = 'H';
	}
}
if(!isset($_GET["DELETE"]) && $id >= 0){
	$stmt ="DELETE FROM MENU_FORMAT
		WHERE MENU_ID = $id";
	$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
	foreach ($category as $selectedOption){
		$stmt ="INSERT INTO MENU_FORMAT (CATEGORY_ID, MENU_ID) VALUES ($selectedOption, $id)";
		$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
	}
}
if(!isset($_GET["DELETE"]) && $_FILES["product_picture"]["name"] <> '' && $id >= 0){
	$allowedExts = array("gif", "jpeg", "jpg", "png");
	$temp = explode(".", $_FILES["product_picture"]["name"]);
	$extension = end($temp);
	if ((($_FILES["product_picture"]["type"] == "image/gif")
		|| ($_FILES["product_picture"]["type"] == "image/jpeg")
		|| ($_FILES["product_picture"]["type"] == "image/jpg")
		|| ($_FILES["product_picture"]["type"] == "image/pjpeg")
		|| ($_FILES["product_picture"]["type"] == "image/x-png")
		|| ($_FILES["product_picture"]["type"] == "image/png"))
		&& in_array($extension, $allowedExts)) {
		if ($_FILES["product_picture"]["error"] > 0) {
			echo "Return Code: ".$_FILES["product_picture"]["error"] . "<br>";
		} else {
			if (file_exists($root."/upload/".$id.'.'.$extension)) {
				unlink($root."/upload/".$id.'.'.$extension);
			}
			move_uploaded_file($_FILES["product_picture"]["tmp_name"], $root."/upload/".$id.'.'.$extension);
			$imagePath = 'http://admin.gevezecafe.com/upload/'.$id.'.'.$extension;
			$stmt = "UPDATE MENU SET PICTURE = ? WHERE ID = ?";
			$st = $db->prepare($stmt);
			$st->bind_param('si', $imagePath, $id);
			if($st->execute()) {
				goToProduct('E');
			}else {
				goToProduct('H');
			}
			$st->close();
		}
	} else {
		echo "Invalid file";
		goToProduct('H');
	}
}else{
	goToProduct($successFlag);
}
disconnect($db);
?>
<?php ob_end_flush(); ?>